package com.loser.module.order.service;

import com.loser.module.order.entity.Order;
import com.loser.module.room.entity.Room;
import com.loser.module.transaction.TS;
import com.loser.module.user.entity.User;
import io.github.loser.properties.MogoDataSourceProperties;
import io.github.loser.properties.MogoLogicProperties;
import io.github.loserya.core.entity.Page;
import io.github.loserya.core.sdk.impl.MogoServiceImpl;
import io.github.loserya.core.sdk.mapper.BaseMapper;
import io.github.loserya.core.wrapper.Wrappers;
import io.github.loserya.global.BaseMapperContext;
import io.github.loserya.global.cache.MapperCache;
import io.github.loserya.global.cache.MongoTemplateCache;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class OrderService extends MogoServiceImpl<Long, Order> {
    public OrderService() {
        System.out.println(2222);
    }

    @Resource
    private ApplicationContext applicationContext;
    @Autowired
    @Qualifier("slave_2MongoTemplate")
    private MongoTemplate mongoTemplate;
    @Resource
    private BaseMapper<Long, Order> orderBaseMapper;
    @Resource
    private BaseMapper<String, User> userBaseMapper;
    @Resource
    private BaseMapper<String, Room> roomBaseMapper;
    @Resource
    private BaseMapper<String, TS> tSBaseMapper;
    @Autowired
    private MongoTemplate slave_1MongoTemplate;
    @Resource
    private MongoTemplate slave_2MongoTemplate;

    public void saveByMapper(Order order) {
        orderBaseMapper.save(order);
    }

    public List<Order> list() {
        return mongoTemplate.findAll(Order.class);
    }

    public Page<Order> queryPage() {
        BaseMapper<Serializable, Order> order = BaseMapperContext.getMapper(Order.class);
        BaseMapper<Serializable, User> user = BaseMapperContext.getMapper(User.class);
        BaseMapper<Serializable, Room> room = BaseMapperContext.getMapper(Room.class);
        BaseMapper<Serializable, TS> ts = BaseMapperContext.getMapper(TS.class);
        MogoDataSourceProperties MogoDataSourceProperties = applicationContext.getBean(MogoDataSourceProperties.class);
        MogoLogicProperties MogoLogicProperties = applicationContext.getBean(MogoLogicProperties.class);
        Map<String, MongoTemplate> cache = MongoTemplateCache.CACHE;
        List<Order> all = slave_1MongoTemplate.findAll(Order.class);
        Object slave1MongoTemplate = applicationContext.getBean("slave_1MongoTemplate");
        System.out.println(slave1MongoTemplate.equals(slave_1MongoTemplate));
        Collection<BaseMapper> baseMappers = BaseMapperContext.getMapper().values();
        Set<Class<?>> mapper = MapperCache.MAPPER;
        Collection<MongoTemplate> values = applicationContext.getBeansOfType(MongoTemplate.class).values();
        return orderBaseMapper.page(Wrappers.empty(), 1, 10);
    }

}
